package com.sanpu.kxe.business.main.presenter;

import android.graphics.Bitmap;
import android.view.View;

import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.sanpu.library.imagechoose.utils.PermissionsUtils;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.adapter.ShareAdapter;
import com.sanpu.kxe.business.main.contract.MyQrcodeContract;
import com.sanpu.kxe.jump.shareutil.ShareUtil;
import com.sanpu.kxe.jump.shareutil.share.ShareListener;
import com.sanpu.kxe.jump.shareutil.share.SharePlatform;
import com.sanpu.kxe.module.dialog.MaterialDialog;
import com.sanpu.kxe.module.dialog.interfaces.OnBtnClickL;
import com.sanpu.kxe.utils.FileUtil;
import com.sanpu.kxe.utils.ToastUtil;

import java.io.File;

import rx.subscriptions.CompositeSubscription;

/**
 * Description：我的二维码
 * Created by 薛勇军 on 2018/8/25.
 */

public class MyQrcodePresenter implements MyQrcodeContract.Presenter {

    private MyQrcodeContract.View mView;

    private ShareAdapter mShareAdapter;

    private CompositeSubscription mSubscription;

    public MyQrcodePresenter(MyQrcodeContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
        mShareAdapter = new ShareAdapter(mView.getContext());
    }

    @Override
    public void saveImage(final Bitmap mClipScreen) {
        final MaterialDialog dialog = new MaterialDialog(mView.getContext());
        dialog.title("提示").content("是否下载二维码图片到相册？").btnText("确定", "取消").show();
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                if (!PermissionsUtils.checkWriteStoragePermission(mView.getContext()))
                    return;
                File result = FileUtil.saveShareImageToGallery(mView.getContext(), mClipScreen);
                ToastUtil.showTextViewPrompt(result != null ? "二维码下载成功!请在相册查看" : "二维码下载失败");
                dialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
            }
        });
    }

    @Override
    public void shareImage(final Bitmap mClipScreen) {
        DialogPlus dialog = DialogPlus.newDialog(mView.getContext())
                .setAdapter(mShareAdapter)
                .setHeader(R.layout.view_dialogplus_header)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        if (position < 0) return;
                        prepareImage(mShareAdapter.getItem(position).getPlatform(), mClipScreen);
                    }
                })
                .setExpanded(false)  // This will enable the expand feature, (similar to android L share dialog)
                .create();
        dialog.show();
    }

    /**
     * 分享图片
     */
    private void prepareImage(final @SharePlatform.Platform int module, Bitmap mClipScreen) {
        ShareListener shareListener = new ShareListener() {
            @Override
            public void shareSuccess() {
//                String shareAppType = "";
//                switch (module) {
//                    case SharePlatform.WX:
//                        shareAppType = CommonConfig.SHARETYPE_WX;
//                        break;
//                    case SharePlatform.WX_TIMELINE:
//                        shareAppType = CommonConfig.SHARETYPE_WXCIRCLE;
//                        break;
//                    case SharePlatform.QQ:
//                        shareAppType = CommonConfig.SHARETYPE_QQ;
//                        break;
//                    case SharePlatform.QZONE:
//                        shareAppType = CommonConfig.SHARETYPE_QZONE;
//                        break;
//                }
//                ToastUtil.showTextViewPrompt("分享成功");
//                if (!TextUtils.isEmpty(shareAppType)) {
//                    Subscription subscription = MainModel.getInstance().sharingIntegral(shareAppType)
//                            .subscribeOn(Schedulers.io())
//                            .unsubscribeOn(Schedulers.io())
//                            .observeOn(AndroidSchedulers.mainThread())
//                            .subscribe(new Observer<String>() {
//                                @Override
//                                public void onCompleted() {
//
//                                }
//
//                                @Override
//                                public void onError(Throwable e) {
//
//                                }
//
//                                @Override
//                                public void onNext(String s) {
//
//                                }
//                            });
//                    mSubscription.add(subscription);
//                }
            }

            @Override
            public void shareFailure(Exception e) {
                ToastUtil.showTextViewPrompt(e.getMessage());
            }

            @Override
            public void shareCancel() {
            }
        };
        ShareUtil.shareImage(mView.getContext(), module, mClipScreen, shareListener);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
